<template>
  <div ref="wrapEl">
    <AdTable @register="registerTable">
      <template #toolbar>
        <Authority :value="'Root.Admin.Wms.IPN.Create'">
          <a-button type="primary" @click="handleCreate">
            <Icon icon="ant-design:plus-circle-outlined" />新增
          </a-button>
        </Authority>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :stop-button-propagation="true"
            :actions="[
              {
                tooltip: '详情',
                icon: 'ant-design:eye-twotone',
                auth: `Root.Admin.Wms.IPN.Read`,
                onClick: handleRead.bind(null, record),
                ifShow: (_action) => {
                  return true;
                },
              },
              {
                icon: 'clarity:note-edit-line',
                auth: 'Root.Admin.Wms.IPN.Update', // 权限控制
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
                ifShow: (_action) => {
                  // if (record.Status == StatusEnum.WaitApply) {
                  //   return true;
                  // }
                  // return false;
                  return true;
                },
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除',
                auth: 'Root.Admin.Wms.IPN.Delete', // 权限控制
                popConfirm: {
                  title: '确认删除？',
                  confirm: handleDelete.bind(null, record),
                },
                ifShow: (_action) => {
                  if (record.Status == StatusEnum.WaitApply) {
                    return true;
                  }
                  return false;
                },
              },
              {
                icon: 'material-symbols:send-rounded',
                auth: 'Root.Admin.Wms.IPN.ApplyWaitToRd', // 权限控制
                tooltip: '提交研发审批',
                ifShow: (_action) => {
                  return true;
                },
                onClick: handleApplyWaitToRd.bind(null, record),
              },
              {
                icon: 'material-symbols:send-rounded',
                color: 'error',
                auth: 'Root.Admin.Wms.IPN.ApplyRdToIpn', // 权限控制
                tooltip: '提交IPN审批',
                ifShow: (_action) => {
                  return true;
                },
                onClick: handleApplyRdToIpn.bind(null, record),
              },
              {
                icon: 'icon-park-outline:back',
                color: 'error',
                auth: 'Root.Admin.Wms.IPN.ApplyRdToWait', // 权限控制
                tooltip: '驳回至研发人员',
                ifShow: (_action) => {
                  return true;
                },
                onClick: handleApplyRdToWait.bind(null, record),
              },
              {
                icon: 'icon-park:setting',
                color: 'success',
                auth: 'Root.Admin.Wms.IPN.ApplyIpnToComplete', // 权限控制
                tooltip: '分配IPN',
                ifShow: (_action) => {
                  return true;
                },
                popConfirm: {
                  title: '审核通过,确认分配IPN?',
                  confirm: handleApplyIpnToComplete.bind(null, record),
                },
              },
              {
                icon: 'icon-park-outline:back',
                color: 'warning',
                auth: 'Root.Admin.Wms.IPN.ApplyIpnToRd', // 权限控制
                tooltip: '驳回至研发主管',
                ifShow: (_action) => {
                  return true;
                },
                onClick: handleApplyIpnToRd.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </AdTable>

    <IPNNoModal @register="registerIpnNoFormModal" title="IPN分配" :reloadFunc="reload" />
    <IPNDetailModal
      @register="registerDetailFormModal"
      :createApi="Create"
      :updateApi="Update"
      :reloadFunc="reload"
    />
    <IPNDescModal
      @register="registerIPNDescModal"
      :unitDicts="unitDicts"
      :projectDicts="projectDicts"
    />
  </div>
</template>

<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { Authority } from '@/components/Authority';
  import { AdColumn, AdTable, useAdTable, TableAction } from '@/components/AdTable';
  import { useLoading } from '@/components/Loading';
  import { useMessage } from '@/hooks/web/useMessage';
  import { FormModeEnum } from '@/enums/formEnum';
  import { AjaxResultTypeEnum } from '@/enums/ajaxResultTypeEnum';
  import { StatusEnum } from './ipn.enum';
  import Icon from '@/components/Icon/Icon.vue';
  import { AjaxResult } from '@/api/model';
  import { useModal } from '@/components/Modal';
  import { columns } from './ipn.data';
  import {
    Read,
    Create,
    Update,
    Delete,
    ApplyWaitToRd,
    ApplyRdToIpn,
    ApplyRdToWait,
    ApplyIpnToRd,
    ApplyIpnToComplete,
    GetMaterialTreeNodes,
    GetDictNodes,
  } from './ipn.api';
  import IPNDetailModal from './IPNDetailModal.vue';
  import IPNDescModal from './IPNDescModal.vue';
  import IPNNoModal from './IPNNoModal.vue';

  export default defineComponent({
    name: 'IPNPage',
    components: {
      Authority,
      AdTable,
      TableAction,
      Icon,
      IPNDetailModal,
      IPNDescModal,
      IPNNoModal,
    },
    setup: (props) => {
      const { createMessage } = useMessage();
      const wrapEl = ref<ElRef>(null);
      const unitDicts = ref<any[]>([]);
      const projectDicts = ref<any[]>([]);

      const DependencyLoad = async function () {
        unitDicts.value = (await GetDictNodes('unit')).Data;
        projectDicts.value = (await GetDictNodes('project')).Data;
      };
      //依赖数据
      DependencyLoad();

      const [openWrapLoading, closeWrapLoading] = useLoading({
        target: wrapEl,
        props: {
          tip: '加载中...',
          absolute: true,
        },
      });

      const [registerTable, { reload, openDetailForm }] = useAdTable({
        // 表标题呈现器
        title: 'IPN申请单列表',
        // 表格数据接口请求对象
        api: Read,
        // 表单列信息 BasicColumn[]
        columns,
        // 是否可拖拽列
        canColDrag: true,
        // 使用搜索表单
        useSearchForm: true,
        // 显示表格设置工具
        showTableSetting: true,
        //表格设置项
        tableSetting: { redo: true, size: true, setting: true, fullScreen: true },
        // 是否显示表格边框
        bordered: false,
        // 是否显示序号列
        showIndexColumn: true,
        // 表格右侧操作列配置 BasicColumn
        actionColumn: {
          width: 380,
          title: '操作',
          dataIndex: 'action',
          fixed: 'right',
        },
      });

      const [registerDetailFormModal, detailFormModalMethods] = useModal();
      async function handleCreate() {
        detailFormModalMethods.openModal(true, {
          formMode: FormModeEnum.Add,
          record: null,
        });
      }

      async function handleEdit(record: Recordable) {
        // openDetailForm(FormModeEnum.Edit, record);
        detailFormModalMethods.openModal(true, {
          formMode: FormModeEnum.Edit,
          record: record,
        });
      }

      const [registerIpnNoFormModal, ipnNoFormModalMethods] = useModal();

      /** 处理删除 */
      async function handleDelete(record: Recordable) {
        const ajaxResult = await Delete([record.Id]);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          reload();
        }
      }

      async function handleApplyWaitToRd(record: Recordable) {
        const ajaxResult = await ApplyWaitToRd(record.Id);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          reload();
        }
      }

      async function handleApplyRdToIpn(record: Recordable) {
        const ajaxResult = await ApplyRdToIpn(record.Id);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          reload();
        }
      }

      async function handleApplyRdToWait(record: Recordable) {
        const ajaxResult = await ApplyRdToWait(record.Id);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          reload();
        }
      }

      async function handleApplyIpnToRd(record: Recordable) {
        const ajaxResult = await ApplyIpnToRd(record.Id);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          reload();
        }
      }

      const [registerIPNDescModal, descModalMethods] = useModal();
      async function handleRead(record: Recordable) {
        descModalMethods.openModal(true, {
          formMode: FormModeEnum.Ok,
          record: record,
        });
      }

      async function handleApplyIpnToComplete(record: Recordable) {
        ipnNoFormModalMethods.openModal(true, {
          formMode: FormModeEnum.Edit,
          record: record,
        });
      }

      return {
        wrapEl,
        registerTable,
        handleCreate,
        handleEdit,
        handleDelete,
        handleApplyWaitToRd,
        handleApplyRdToIpn,
        handleApplyRdToWait,
        handleApplyIpnToRd,
        handleApplyIpnToComplete,
        handleRead,
        registerDetailFormModal,
        registerIPNDescModal,
        Create,
        Update,
        reload,
        unitDicts,
        projectDicts,
        registerIpnNoFormModal,
        StatusEnum,
      };
    },
  });
</script>
